
function DateFormat(date , fmt){
	  var o = {
		"M+" : date.getMonth() + 1, //月份   
		"d+" : date.getDate(), //日   
		"h+" : date.getHours(), //小时   
		"H+" : date.getHours(), //小时  
		"m+" : date.getMinutes(), //分   
		"s+" : date.getSeconds(), //秒   
		"q+" : Math.floor((date.getMonth() + 3) / 3), //季度   
		"S" : date.getMilliseconds() //毫秒   
	};
	if (/(y+)/.test(fmt)){
		fmt = fmt.replace(RegExp.$1, (date.getFullYear() + "").substr(4 - RegExp.$1.length));
	}
	for ( var k in o){
		if (new RegExp("(" + k + ")").test(fmt)){
			fmt = fmt.replace(RegExp.$1, (RegExp.$1.length == 1) ? (o[k]): (("00" + o[k]).substr(("" + o[k]).length)));
		}
	}
	return fmt;   
}

function checkAllRow(checkboxAllObj,checkboxName){
	var checkboxObjs = jQuery('input[name="'+checkboxName+'"]');
	if(checkboxObjs!=null&&checkboxObjs.length>0){
		var chk = jQuery(checkboxAllObj);
		if(chk[0].checked==true){
			checkboxObjs.attr("checked",'true');
		}else{
			checkboxObjs.removeAttr("checked");
		}
	}
}

//queryAppPanel 查询数据
function queryAppData(queryAppPanel,urls){
	var queryAppPanelObj = document.getElementById(queryAppPanel);
	var jsonStr =  getQueryCondition(queryAppPanelObj);
	var appData = null;
	params = {query:jsonStr};
	jQuery.ajax({
		type: "post",url:urls,async:false,data:params,
		dataType:'text',
		complete: function(){
		},
		//请求成功时执行的回调函数
		success: function(respone){
			appData = eval(respone);
		},
		error : function(){
	          alert("异常！"); 
		}
	});
	var tableObj = document.getElementById('AppData');
	initTableData(appData,tableObj);
}

//获取查询条件
function getQueryCondition(queryAppPanelObj){
	//var queryConditionInputs = queryAppPanelObj.getElementsByTagName('input');
	var AppVoForm = jQuery(queryAppPanelObj).children()[0];
	if(AppVoForm==null||AppVoForm.length<=0){
		return null;
	}
	var queryConditionJsonStr = '';
	queryConditionJsonStr +='{';
	for(var i = 0 ;i <AppVoForm.length;i++ ){
		if(i>0){
			queryConditionJsonStr +=',';
		}
	    var inputText = AppVoForm[i];
	    var key = inputText.name;
	    var value = inputText.value;
	    queryConditionJsonStr +=key+':"'+value+'"';
	}
	queryConditionJsonStr +='}';
	return queryConditionJsonStr;
}

//初始化表单数据
function initTableData(data,tableObj){
	var dataStr = '';
	if(data!=null&&data.length>0){
		for(var i = 0;i < data.length ; i++){
			var json = data[i];
			dataStr+='<tr>';
			dataStr+='<td class="allCheckbox">';
			dataStr+='<input name="rolCheckBox" type="checkbox" value="'+json.id+'">';
			dataStr+='</td>';
			dataStr+='<td class="serilaz">';
			dataStr+='<input type="text" value="'+(i+1)+'" name="no">';
			dataStr+='</td>';
			dataStr+='<td>';
			dataStr+='<input type="text" value="'+json.id+'" name="id">';
			dataStr+='</td>';
			dataStr+='<td>';
			dataStr+='<input type="text" value="'+json.appManageName+'" name="AppManageName">';
			dataStr+='</td>';
			dataStr+='<td>';
			dataStr+='<input type="text" value="'+json.appManageCode+'" name="AppManageCode">';
			dataStr+='</td>';
			dataStr+='<td>';
			dataStr+='<input type="text" value="'+json.appManageURL+'" name="AppManageURL">';
			dataStr+='</td>';
			dataStr+='<td>';
			dataStr+='<input type="text" value="'+json.appManageDesc+'" name="AppManageDesc">';
			dataStr+='</td>';
			dataStr+='<td>';
			dataStr+='<input type="text" value="'+json.appManageEvent+'" name="AppManageEvent">';
			dataStr+='</td>';
			dataStr+='';
			dataStr+='</tr>';
		}
		jQuery(tableObj).html(dataStr);
	}
}
/**
 * 初始化编辑页面
 * @param data
 * @param form
 */
function initEditPage(data,forms){
	var AppVoForm = forms;
	if(AppVoForm==null||AppVoForm.length<=0){
		return null;
	}
	for(var i = 0 ;i <AppVoForm.length;i++ ){
	    var inputText = AppVoForm[i];
	    var key = inputText.name;
	    var value = data[key];
	    inputText.value = value;
	}
}
/**
 * 新增
 * @param addPageUrl
 */
function addAppData(addPageUrl){
	var url  = addPageUrl; 
	var title = "新增";
	var id = "window"+makeRandom(10);
	var width = 900;
	var height = 400;
	var zIndex = 1000;
	openWindow(id,title,url,width,height,zIndex);
	windowId = id;
}

/**
 * 删除
 * @param urls
 * @param params
 * @param id
 */
function delelteData(urls,params,id,func){
	var appData =  {};
	jQuery.ajax({
		type: "post",url:urls,async:false,data:params,
		//dataType:'json',
		complete: function(){
		},
		//请求成功时执行的回调函数
		success: function(respone){
			appData = eval(respone);
			if(func!=null){
				func();
			}
		},
		error : function(){
	          alert("异常！"); 
		}
	});
	var tableObj = document.getElementById(id);
	initTableData(appData,tableObj);
}

/**
 * 获取表单数据
 * @param formObj
 * @returns
 */
function getFormData(formObj){
	if(formObj!=null&&formObj.length>0){
		var json = {};
		for( var i = 0; i<formObj.length; i++){
			var e = formObj[i];
			if(e!=null){
				json[e.name] = e.value;
			}
		}
		return json;
	}
	return null;
}

/**
 * 获取父页面对应的iframe,
 * 此方法仅适用于非跨域的iframe嵌套
 * @returns iframe
 */
function getParentPageIframe(){
	var buildId = navigator.buildID; //获取当前页面的标识
	var iframes =jQuery('iframe',parent.document);  //获取父页面所有的iframe
	if(iframes!=null&&iframes.length>0){
		for( var i = 0; i<iframes.length;i++){
			var id = iframes[i].contentWindow.navigator.buildID;
			if(id == buildId){
				//通过对比Id，查找到当前页面对应的父页面的iframe
				return iframes[i];
			}
		}
	}
	return null;
}
/**
 * 产生随机数
 * @param n
 * @returns
 */
function makeRandom(n){
	var pows = Math.pow(10,n);
	var floats = Math.random();
	var number = Math.floor(floats*pows);
	return number;
}

/**
 * String To JSON
 * @param Str
 * @returns
 */
function StringToJson(Str){
	if(Str==null||Str ==''){
		return null;
	}
	var json = null;
	if(Str.substr(0,1)=='{'){
		json = eval('['+Str+']')[0];
	}else{
		json  = eval(Str);
	}
	return json;
}

/**
 * json 转 String
 */
function JsonToString(o) {
    var arr=[];
    var fmt = function(s) { 
            if (typeof s == 'object' && s != null ) return JsonObjectToString(s); 
            return /^(string|number)$/.test(typeof s) ? "\"" + s + "\"" : s; 
    };
    
    if(o instanceof Array){
        for (var i in o){
                arr.push(fmt(o[i]));
        }
        return '[' + arr.join(',') + ']';
            
    }
    else{
        for (var i in o){
                arr.push("\"" + i + "\":" + fmt(o[i]));
        }
        return '{' + arr.join(',') + '}'; 
    }
}; 

/**
 * 
 * URL处理
 * @param url
 * @param paramName
 * @param paramValue
 * @returns
 */
function addUrlParam(url,paramName,paramValue){
    if(url==null||url==''){
        return null;
    }
    if(paramName==null||paramName==''){
        return url;
    }
    if(url.indexOf('?')>0){
        url+='&'+paramName+'='+paramValue;
    }else{
        url+='?'+paramName+'='+paramValue;
    }
    return url;
} 

/**
 * 获取对象的宽度和位置信息
 * @param obj
 * @returns
 */
function getElementPos(obj){
	if(obj==null){
		return null;
	}
	var objPos = {};
	var msgWidth = obj.scrollWidth;
	var msgHeight= obj.scrollHeight;
	var left = obj.offsetLeft;
	var top = obj.offsetTop;
	objPos.width = msgWidth;
	objPos.height = msgHeight;
	objPos.left = left;
	objPos.top = top;
	return objPos;
}

/**
 * 获取页面宽度信息
 * @param obj 页面对象 value : (this , parent)
 * @returns windowPos 窗体信息
 */
function getWindowPos(obj){
	if(obj==null){
		obj = this;
	}
	//窗体信息
	var windowPos = {};
	
	//被滚动条卷曲的宽度
    var bgLeft=obj.window.pageXOffset   
			    || obj.document.documentElement.scrollLeft   
			    || obj.document.body.scrollLeft || 0;
    windowPos.left = bgLeft;
    
    //页面可视宽度
    var bgWidth=obj.document.documentElement.clientWidth   
    			||obj.document.body.clientWidth || 0;
    windowPos.width = bgWidth;
    
    //被滚动条卷曲的高度
    var bgTop=obj.window.pageYOffset   
			    || obj.document.documentElement.scrollTop   
			    || obj.document.body.scrollTop || 0;
    windowPos.top = bgTop;
    //页面可视高度
	var bgHeight=obj.document.documentElement.clientHeight   
				|| obj.document.body.clientHeight || 0;
	windowPos.height = bgHeight;
    return windowPos;
}

/**
 * 关闭窗口
 * @param windowId iframe中存储的窗口的IDs属性名
 */
function closeAddpage(windowId){
	var iframeObj = getParentPageIframe();
	windowId = jQuery(iframeObj).attr(windowId);
	parent.removeWindow(windowId);
}

/**
 * 关闭窗口--销毁
 * @param id
 */
function removeWindow(id){
	var windowObj = typeof(id)=="object"?id:document.getElementById(id);
	if(windowObj!=null){
		windowObj.parentNode.removeChild(windowObj);
	}
}
/**
 * 创建弹出窗口
 */
function openWindow(id,title,url,width,height,zIndex,params){
	
	var jsonString = JsonToString(params);
	//jsonString = encodeURI(encodeURI(jsonString));
	jsonString = encodeURI(jsonString);
	var urls = addUrlParam(url,'params',jsonString);
	//创建弹出框的外层DIV
	var windowObj = document.createElement("div");
	
	//相关参数
	var name = "windowIframe";
	var className = "windowCss";
	var src = urls;
	var window_div_id = "winDiv"+makeRandom(8);
	var window_title_id = "winTtitle"+makeRandom(8);
	var window_iframe_id = "winIframe"+makeRandom(8);
	//设置弹出框的属性
	windowObj.id = id;  //id
	windowObj.name = name;  //名字
	windowObj.className = className;  //类名
	windowObj.style.width = width+'px';  //宽度
	windowObj.style.height = height+'px';  //高度
	windowObj.style.position   = "absolute";  //绝对定位
	windowObj.style.zIndex = zIndex;  //垂直高度
	windowObj.style.display='block';  //显示
	
	//设置边框
	var html = '';
	html += '<div class="window_alpha"></div>';
	html += '<div id = "'+window_div_id+'" class="">';
	html += '<div id = "'+window_title_id+'" class="window_top">';
	html += '<span class="window_span_title">'+title+'</span>';
	html += '<i class="window_i_close" onclick="removeWindow('+id+')"></i>';
	html += '</div>';
	html += '<iframe id="'+window_iframe_id+'" src="'+src+'" windowId="'+id+'" name="" class=""></iframe>';
	html += '';
	html += '';
	html += '';
	html += '';
	html += '</div>';
	
	windowObj.innerHTML = html;
	document.body.appendChild(windowObj);
	setObjMiddleXs(windowObj);
	setObjMiddleYs(windowObj);
	//setObjMiddle(msgObj,bgWidth,bgHeight)
	
	
	//根据各个浏览器，设置宽度和高度
	var window_div_width = 0;
	var window_div_height = 0;
	var window_iframe_width = 0;
	var window_iframe_height = 0;
	var ieDocumentMode = document.documentMode; //IE的几种模式
	//alert(ieDocumentMode);
	var key = ieDocumentMode;
	switch(key){
		case 10:
			window_iframe_width = width-14;
			window_iframe_height = height-46;
			
			window_div_width = width-13;
			window_div_height = height-12;
			break;
		case 9:
			window_iframe_width = width-14;
			window_iframe_height = height-46;
			
			window_div_width = width-13;
			window_div_height = height-12;
			break;
		case 8:
			window_iframe_width = width-14;
			window_iframe_height = height-44;
			window_div_width = width-13;
			window_div_height = height-12;
			break;
		case 7:
			window_iframe_width = width-14;
			window_iframe_height = height-46;
			
			window_div_width = width-10;
			window_div_height = height-12;
			break;
		case 6:
			window_iframe_width = width-14;
			window_iframe_height = height-46;
			
			window_div_width = width-13;
			window_div_height = height-12;
			break;
		case 5:
			window_iframe_width = width-18;
			window_iframe_height = height-46;
			
			window_div_width = width-13;
			window_div_height = height-12;
			break;
		
		default:
			window_iframe_width = width-14;
			window_iframe_height = height-46;
			
			window_div_width = width-13;
			window_div_height = height-12;
			break;
	}
	
	//窗口内层DIV
	var window_div = document.getElementById(window_div_id);
	window_div.className = "window_div";  //类名
	window_div.style.width = window_div_width +'px';  //宽度
	window_div.style.height = window_div_height +'px';  //高度
	
	//窗口标题
	var window_title = document.getElementById(window_title_id);
	
	//窗口内层的Iframe
	var window_iframe = document.getElementById(window_iframe_id);
	window_iframe.className = "window_frame_page";  //类名
	window_iframe.style.width = window_iframe_width+'px';  //宽度
	window_iframe.style.height = window_iframe_height+'px';  //高度
	
	jQuery(document).ready(function(){
		
		mouseDragdropHandler.register(window_title,windowObj);
		//dragPrototype(window_title,windowObj);
	});
}

/*================居中显示对象==================*/
function setObjMiddle(msgObj,bgWidth,bgHeight){
	 var msgWidth = msgObj.scrollWidth;  
	 var msgLeft=0; 
	 if(bgWidth>msgWidth){
    	msgLeft=Math.round((bgWidth-msgWidth)/2);
     }else{
    	msgLeft=10;
     }
	 msgObj.style.position = "absolute";  
	    msgObj.style.left  = msgLeft+"px"; 
	    
	    var msgHeight= msgObj.scrollHeight;    
	    var msgTop=0;  
	    if(bgHeight>msgHeight){
	    	msgTop=Math.round((bgHeight-msgHeight)/2);
	    }else{
	    	msgTop=10;
	    }
	    msgObj.style.position = "absolute";  
	    msgObj.style.top      = msgTop+"px"; 
	    
}

function setObjMiddleXs(msgObj){
    var msgWidth = msgObj.scrollWidth;  
    var bgLeft=window.pageXOffset   
               || document.documentElement.scrollLeft   
               || document.body.scrollLeft || 0;  
    var bgWidth=document.documentElement.clientWidth   
               ||document.body.clientWidth || 0;
    var msgLeft=0; 
    if(bgWidth>msgWidth){
    	msgLeft=bgLeft+Math.round((bgWidth-msgWidth)/2);
    }else{
    	msgLeft=10;
    }
    msgObj.style.position = "absolute";  
    msgObj.style.left  = msgLeft+"px";  
}
function setObjMiddleYs(msgObj){
    var msgHeight= msgObj.scrollHeight;  
    var bgTop=window.pageYOffset   
            || document.documentElement.scrollTop   
            || document.body.scrollTop || 0;  
    var bgHeight=document.documentElement.clientHeight   
            || document.body.clientHeight || 0;   
    var msgTop=0;  
    if(bgHeight>msgHeight){
    	msgTop=bgTop+Math.round((bgHeight-msgHeight)/2);
    }else{
    	msgTop=10;
    }
    msgObj.style.position = "absolute";  
    msgObj.style.top      = msgTop+"px";  
}


mouseDragdropHandler={
		register:function(handler,target,cursor){ // 注册鼠标移动的一些事件。
			var mouseDownFlag=false,mouseClickLeft=0,mouseClickTop=0;
			var mouseHandlerObj=this.get(handler); //鼠标操作对象
			var mouseDragdropObj=this.get(target); //鼠标拖动对象
			var NS=navigator.appName=='Netscape';//当前浏览器的类型 Netscape ,Microsoft Internet Explorer
			
			function startDrag(evt){ // 按下鼠标左键时的事件。
				 evt=window.event||evt;  // 获取当前事件对象。
				 if(evt.button==((NS)?0 :1))  { //IE 左击值为1,FireFix和Chrome为0
					 if(!NS){
						 //mouseDragdropObj.setCapture();
					 }
					 mouseDownFlag=true;  // 记录已经准备开始移动了。
					 mouseClickLeft=evt.clientX-parseInt(mouseDragdropObj.style.left); // 记录当前坐标轴。
					 mouseClickTop=evt.clientY-parseInt(mouseDragdropObj.style.top);
					 //jQuery('.objMoveZindex').removeClass('objMoveZindex');
					 //jQuery(mouseDragdropObj).addClass('objMoveZindex');
		 			 document.onmouseup=endDrag;  // 鼠标释放事件。
			         document.onmousemove=doDrag; // 鼠标移动事件。
			         mouseHandlerObj.style.cursor=cursor||"move";
				 }
			}
			
			function doDrag(evt){ // 鼠标开始移动时的事件。
				 evt=window.event||evt; // 获取当前事件对象。
				 if(!mouseDownFlag){
					 return false; // 如果mouseDownFlag不等于真了返回。
				 }
				 var mx = evt.clientX-mouseClickLeft;
				 var my = evt.clientY-mouseClickTop;
				 //mx = mx>0?mx:0;
				 //my = my>0?my:0;
				 
				 //var mb = getMouseX(mouseDragdropObj);
				 //var mr = getMouseY(mouseDragdropObj);
				 //mx = mx>mr?mr:mx;
				 //my = my>mb?mb:my;
				 mouseDragdropObj.style.left=mx+"px"; // 把鼠标当前移动的位置赋值给div
				 mouseDragdropObj.style.top=my+"px"; // 当前位置减去开始位置就是层当前存放的位置。
			}
			
			function endDrag(){ // 释放鼠标左键时的事件。
				 if(mouseDownFlag){ // 如果mouseDownFlag还为真那么就赋值为假。
					if(this.isIE) {
						mouseDragdropObj.releaseCapture(); //该函数从当前的窗口释放鼠标捕获，并恢复通常的鼠标输入处理。
					}
					mouseDownFlag=false;
					mouseHandlerObj.style.cursor="";
				 }
			}
			
/*			function getMouseX(msgObj){
			    var msgWidth = msgObj.scrollWidth;  
			    var bgLeft=window.pageXOffset   
			               || document.documentElement.scrollLeft   
			               || document.body.scrollLeft || 0;  
			    var bgWidth=parent.document.documentElement.clientWidth   
			               ||document.body.clientWidth || 0;
			    var msgLeft=bgLeft+bgWidth-msgWidth; 
			    return msgLeft;
			}
			function getMouseY(msgObj){
			    var msgHeight= msgObj.scrollHeight;  
			    var bgTop=window.pageYOffset   
				            || document.documentElement.scrollTop   
				            || document.body.scrollTop || 0;  
			    var bgHeight=parent.document.documentElement.clientHeight   
			            || document.body.clientHeight || 0;   
			    var msgTop=bgTop+bgHeight-msgHeight;  
			    return msgTop;
			}*/
			
			mouseHandlerObj.onmousedown=startDrag; // 鼠标按下事件。
			mouseDragdropObj.onselectstart=function(){
				return false;// 禁止选择和右键菜单。
			}; 
			mouseDragdropObj.oncontextmenu=function(){
				return false;// 禁止选择和右键菜单。
			}; 
		},
		 isIE:(navigator.appName=="Microsoft Internet Explorer"), // 判断是否为IE。
		 get:function(element){ // 通过一串字符返回一个对象。
			if(typeof(element) == "string"){
			 	return document.getElementById(element);
			}
			return element;
		}
};

//原生JS方法定义的拖动
function dragPrototype(dragControl, dragContent){
    dragControl=typeof(dragControl)=="object" ? dragControl:document.getElementById(dragControl);
    dragContent=typeof(dragContent)=="object" ? dragContent:document.getElementById(dragContent);
 var _x = 0, _y = 0, _drag = false, cw, ch, sw, sh;
 dragContent = typeof dragContent == "undefined" ? dragControl : dragContent;  //如果不存在第二个参数就把拖动对象设置为第一个参数
 
 dragControl.onmousedown = function(e){
     _drag = true;

     e = window.event?window.event:e;
     cw = document.documentElement.clientWidth;
     ch = document.documentElement.clientHeight;
     sw = parseInt(dragContent.offsetWidth);
     sh = parseInt(dragContent.offsetHeight);

     _x = e.clientX - dragContent.offsetLeft;  // 获取当前鼠标相对div原点的坐标
     _y = e.clientY - dragContent.offsetTop;
      
     document.onmousemove = function(e){
         if(_drag){
             e = window.event?window.event:e;
             window.getSelection ? window.getSelection().removeAllRanges() : document.selection.empty(); // 禁止拖放对象文本被选择的方法
             document.body.setCapture && dragContent.setCapture(); // IE下鼠标超出视口仍可被监听
              
             var x = e.clientX - _x;
             var y = e.clientY - _y;
              
             x = x < 0 ? x = 0 : x < (cw-sw) ? x :(cw-sw); // 是否超出窗口的判断
             y = y < 0 ? y = 0 : y < (ch-sh) ? y :(ch-sh);
  
             dragContent.style.left = x + "px";
             dragContent.style.top = y + "px";
         };
     };
      
     document.onmouseup = function(){
         _drag = false;
         document.body.releaseCapture && dragContent.releaseCapture();
     };
 };
}

//Flash



